﻿using System;
using System.Reflection;

namespace Gonte.Reflection
{
    /// <summary>
    /// Traverses an object using reflection
    /// </summary>
    public static class ObjectTraversal
    {
        /// <summary>
        /// Traverses the public complex properties of an object
        /// </summary>
        /// <param name="object"></param>
        public static void TraverseComplexProperties(Object @object, TraverseComplexPropertyHandler traverseComplexProperty)
        {
            ExceptionThrower.ThrowWhenNull("object", @object);
            ExceptionThrower.ThrowWhenNull("traverseComplexProperty", traverseComplexProperty);

            TypeAccessor typeAccessor = TypeAccessor.GetTypeAccessor(@object.GetType());

            foreach (string propertyName in typeAccessor.PropertyNames)
            {
                if (!typeAccessor.IsPrimitive(propertyName)) // Complex property
                {
                    traverseComplexProperty(typeAccessor, propertyName);
                }
            }
        }

        public delegate void TraverseComplexPropertyHandler(TypeAccessor typeAccessor, string propertyName);
    }
}
